<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="" name="keywords">
    <meta content="" name="description">
    <title th:text="用户修改"></title>
    <link rel="stylesheet" th:href="@{/component/pear/css/pear.css}">
</head>
<body>
<form action="" class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">ID</label>
                    <div class="layui-input-inline">
                        <input autocomplete="off" class="layui-input" lay-verify="title" name="id"
                               placeholder="请输入ID" th:value="${oldUserInfo.getId()}" type="text" disabled="disabled">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input id="username" autocomplete="off" oninput="checkUserName()" class="layui-input"
                               lay-verify="title" name="username"
                               placeholder="请输入用户名" th:value="${oldUserInfo.getUsername()}" type="text">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="layui-anim layui-anim-up" style="font-size: small;color: red"
                              hidden>该用户名已被使用</span>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">昵称</label>
                    <div class="layui-input-inline">
                        <input autocomplete="off" class="layui-input" lay-verify="title" name="nickname"
                               placeholder="请输入昵称" th:value="${oldUserInfo.getNickname()}" type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-inline">
                        <input autocomplete="off" class="layui-input" lay-verify="title" name="password"
                               placeholder="请输入密码" th:value="${oldUserInfo.getPassword()}" type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-inline">
                        <input autocomplete="off" class="layui-input" lay-verify="email" name="email"
                               placeholder="请输入邮箱" th:value="${oldUserInfo.getEmail()}" type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">权限</label>
                    <div class="layui-input-inline">
                        <select name="role">
                            <option value="">请选择用户权限</option>
                            <option value="root" th:selected="${oldUserInfo.getRole()=='root'}">
                                超级管理员
                            </option>
                            <option value="admin" th:selected="${oldUserInfo.getRole()=='admin'}">
                                管理员
                            </option>
                            <option value="user" th:selected="${oldUserInfo.getRole()=='user'}">
                                普通用户
                            </option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-filter="user-update" lay-submit=""
                    type="submit">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button class="pear-btn pear-btn-sm" type="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script th:src="@{/component/layui/layui.js}"></script>
<script th:src="@{/component/pear/pear.js}"></script>
<script th:src="@{/admin/js/jquery.min.js}"></script>
<script th:inline="javascript">
    let basePath = [[${#httpServletRequest.getContextPath()}]];
    layui.use(['form', 'jquery'], function () {
        let form = layui.form;
        let $ = layui.jquery;

        form.on('submit(user-update)', function (data) {
            $.ajax({
                url: basePath + '/user/update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    if (result.code) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
        window.checkUserName = function () {
            let username = $('#username');
            if (username.val() == [[${oldUserInfo.getUsername()}]]) {
                return;
            }
            $.ajax({
                type: "get",
                url: basePath + '/user/queryByUsername',
                data: {'username': username.val()},
                dataType: 'json',
                success: function (result) {
                    if (result.code == 1) {
                        //成功
                        if (result.data[0] == true) {
                            username.parent().next().children("span").show();
                        } else {
                            username.parent().next().children("span").hide();
                        }
                    } else {
                        //失败
                        console.log(result.msg);
                    }
                },
                error: function (arg) {
                    layui.layer.msg(arg);
                }
            });
        }
    })
</script>
</body>
</html>